const userModel = require('../models/userModel');
const response = require('../utils/response');

// 获取用户信息
exports.getUser = async (req, res, next) => {
  try {
    // 从请求中获取用户ID（假设通过查询参数传递）
    const { userId } = req.query;
    
    if (!userId) {
      return response.error(res, '缺少用户ID参数', 400);
    }

    // 查询用户数据
    const user = await userModel.getUserById(userId);
    
    if (!user) {
      return response.error(res, '用户不存在', 404);
    }

    response.success(res, '获取用户成功', user);
  } catch (error) {
    next(error);
  }
};

// 添加用户
exports.addUser = async (req, res, next) => {
  try {
    const { name, age, email } = req.body;
    
    // 验证必填字段
    if (!name || !age || !email) {
      return response.error(res, '缺少必要参数', 400);
    }

    // 创建新用户
    const newUser = await userModel.createUser({
      name,
      age,
      email,
      createdAt: new Date()
    });

    response.success(res, '添加用户成功', newUser);
  } catch (error) {
    next(error);
  }
};